package repositories;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import users.Person;
import books.Loan;

/**
 */
public class LoanRepository extends HibernateGenericDAO<Loan> implements GenericRepository<Loan> {

    private static final long serialVersionUID = 1L;

    @Override
    protected Class<Loan> getDomainClass() {
        return Loan.class;
    }

    @SuppressWarnings("unchecked")
    public List<Loan> getCurrentLoansOf(final Person aPerson) {
        return (List<Loan>) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public List<Loan> doInHibernate(final Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Loan.class);
                criteria.add(Restrictions.eq("loaner", aPerson));
                criteria.add(Restrictions.eq("finished", false));
                return criteria.list();
            }
        });
    }

}
